<?php

/**
 * Description of ClassLoader
 *
 * @author prometheus
 */
class x10_NamespaceManager_ClassLoader extends x10_GeneralClass
{
	public $xName = 'Osztálybetöltő';

	/**
	 * Osztálybetöltési adatszolgáltató
	 * @var x10_NamespaceManager_ClassLoaderProvider
	 */
	protected $provider;

	public function __construct(x10_NamespaceManager_ClassLoaderProvider $provider)
	{
		$this->provider = $provider;
	}

	public function locate($className)
	{
		$classRelativePath = substr(str_replace('_', '/', $className), strpos($className, '_')).'.php';
		$path = str_replace('//', '/', $this->provider->basePath.$classRelativePath);
		$alterPath = str_replace('//', '/', $this->provider->alterPath.$classRelativePath);
		$isExists = FALSE;
		$isAccessible = FALSE;

		if (file_exists($path) or file_exists($alterPath))
		{
			$isExists = TRUE;
			if (file_exists($alterPath))
			{
				$path = $alterPath;
			}
		}
		
		if (is_file($path) and is_readable($path))
		{
			$isAccessible = TRUE;
		}

		if (!$isExists)
		{
			throw new x10_E_NamespaceManager_ClassLoader_SourceNotFound($className, $path);
		}
		else if (!$isAccessible)
		{
			throw new x10_E_NamespaceManager_ClassLoader_SourceUnaccessible($className, $path);
		}
		else
		{
			return $path;
		}
	}
}

?>